package com.gcit.seniorWish.web.modular.inputMidScore.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.NumberUtil;
import com.gcit.seniorWish.core.cache.CacheKit;
import com.gcit.seniorWish.web.common.constant.cache.Cache;
import com.gcit.seniorWish.web.common.constant.cache.CacheKey;
import com.gcit.seniorWish.web.common.persistence.model.SeniorExaminee;
import com.gcit.seniorWish.web.core.shiro.ShiroUser;
import com.gcit.seniorWish.web.modular.examinee.service.ISeniorExamineeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.web.common.persistence.dao.ExamineeScoreMapper;
import com.gcit.seniorWish.web.common.persistence.model.ExamineeScore;
import com.gcit.seniorWish.web.modular.inputMidScore.service.IExamineeScoreService;
import com.gcit.seniorWish.web.modular.inputMidScore.transfer.ExamineeScoreDto;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

@Service
public class ExamineeScoreServiceImpl extends ServiceImpl<ExamineeScoreMapper, ExamineeScore>
		implements IExamineeScoreService {

	@Autowired
	private ISeniorExamineeService seniorExamineeService;


	/**
	 * 根据模板导入中考成绩信息
	 *
	 * @param inputStream
	 * @param shiroUser
	 * @return
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
	@Override
	@Transactional
	public String importMidScore(InputStream inputStream, ShiroUser shiroUser) {
		StringBuffer msg = new StringBuffer();
		ExcelReader reader = ExcelUtil.getReader(inputStream);
		Map<String, String> headerAlias = new HashMap<String, String>();
		headerAlias.put("准考证号", "examCardNumber");
		headerAlias.put("考生姓名", "studentName");
		headerAlias.put("考场号", "examSiteNumber");
		headerAlias.put("座位号", "seatNumber");
		headerAlias.put("建档号", "archivesNumber");
		headerAlias.put("中学名称", "senoirSchoolName");
		headerAlias.put("语文", "chinese");
		headerAlias.put("数学", "math");
		headerAlias.put("英语", "english");
		headerAlias.put("人机对话", "englishListen");
		headerAlias.put("物理", "physics");
		headerAlias.put("化学", "chemistry");
		headerAlias.put("历史", "history");
		headerAlias.put("政治", "politics");
		headerAlias.put("体育", "sport");
		//加分类型不需要
		headerAlias.put("加分", "scorePlus");
//		headerAlias.put("加分类型", "scorePlusName");
		headerAlias.put("中考总分", "senoirTotalScore");
		reader.setHeaderAlias(headerAlias);
		List<ExamineeScoreDto> excelData = reader.readAll(ExamineeScoreDto.class);
		if (excelData.size() > 0) {
			Iterator excelIt = excelData.iterator();
			ExamineeScoreDto item = null;
			int i = 0;
			//循环列表中的记录
			while (excelIt.hasNext()) {
				i++;
				item = (ExamineeScoreDto) excelIt.next();
				//判断必填内容是否填写
				if (StrUtil.isBlank(item.getExamCardNumber())) {
					msg.append("第" + i + "条缺少准考证编号\n");
					continue;
				} else {
					//检查准考证号是否已经存在 如果存在的话 不允许插入
					if (baseMapper.selectCount(Condition.create().eq("exam_card_number", item.getExamCardNumber())) > 0) {
						msg.append("第" + i + "条准考证编号成绩已存在\n");
						continue;
					}
				}
				if (StrUtil.isBlank(item.getStudentName())) {
					msg.append("第" + i + "条缺少学生姓名\n");
					continue;
				}
			}

			if (StrUtil.isBlank(msg.toString())) {
				// 数据保存
				for (ExamineeScoreDto es : excelData) {
					ExamineeScore examineeScore = new ExamineeScore();
					examineeScore.setArchivesNumber(es.getArchivesNumber());
					examineeScore.setExamCardNumber(es.getExamCardNumber());
					examineeScore.setExamSiteNumber(es.getExamSiteNumber());
					examineeScore.setSeatNumber(es.getSeatNumber());
					examineeScore.setSenoirSchoolName(es.getSenoirSchoolName());
					examineeScore.setStudentName(es.getStudentName());
//					examineeScore.setGender(es.getGender());
					examineeScore.setChemistry(NumberUtil.isNumber(es.getChemistry()) ? new BigDecimal(es.getChemistry()) : null);
					examineeScore.setChinese(NumberUtil.isNumber(es.getChinese()) ? new BigDecimal(es.getChinese()) : null);
					examineeScore.setEnglish(NumberUtil.isNumber(es.getEnglish()) ? new BigDecimal(es.getEnglish()) : null);
					examineeScore.setEnglishListen(NumberUtil.isNumber(es.getEnglishListen()) ? new BigDecimal(es.getEnglishListen()) : null);
					examineeScore.setHistory(NumberUtil.isNumber(es.getHistory()) ? new BigDecimal(es.getHistory()) : null);
					examineeScore.setMath(NumberUtil.isNumber(es.getMath()) ? new BigDecimal(es.getMath()) : null);
					examineeScore.setPhysics(NumberUtil.isNumber(es.getPhysics()) ? new BigDecimal(es.getPhysics()) : null);
					examineeScore.setPolitics(NumberUtil.isNumber(es.getPolitics()) ? new BigDecimal(es.getPolitics()) : null);
					examineeScore.setSenoirTotalScore(NumberUtil.isNumber(es.getSenoirTotalScore()) ? new BigDecimal(es.getSenoirTotalScore()) : null);
					examineeScore.setSport(NumberUtil.isNumber(es.getSport()) ? new BigDecimal(es.getSport()) : null);
					examineeScore.setScorePlus(NumberUtil.isNumber(es.getScorePlus()) ? new BigDecimal(es.getScorePlus()) : null);
					//加分类型不需要
//					examineeScore.setScorePlusName(es.getScorePlusName());
					baseMapper.insert(examineeScore);
//					CacheKit.put(Cache.EXAMINEE_SCORE,CacheKey.EXAMINEE_SCORE_BY_EXAM_CARD+examineeScore.getExamCardNumber(),examineeScore);
				}
			}
		} else {
			msg.append("导入模板为空\n");
		}
		return msg.toString();
	}

	/**
	 * 通过准考证号查询学生成绩
	 * @param examCardNumber
	 * @return
	 */
	@Override
//	@Cacheable(value = Cache.EXAMINEE_SCORE, key = "'" + CacheKey.EXAMINEE_SCORE_BY_EXAM_CARD + "'+#examCardNumber")
	public ExamineeScore getExamineeScore(String examCardNumber){
		ExamineeScore score = new ExamineeScore();
		score.setExamCardNumber(examCardNumber);
		score=this.baseMapper.selectOne(score);
		return score;
	}
}
